﻿package toolbar {
	import flash.display.Sprite;
	import toolbar.mode.ModeGroup;
	import toolbar.system.SystemGroup;
	import toolbar.operate.OperateGroup;
	import toolbar.biu.BIUGroup;
	import toolbar.align.AlignGroup;
	import toolbar.rudo.DoGroup;
	import toolbar.font.FontGroup;
	
	public class ToolBar extends Sprite {
		private var groupList:Array; //索引数组，以按钮组名字为键存放所有按钮组
		private var groupMap:Array; //关联数组，存放所有按钮组
		private var left:int;
		private var top:int;
		private var maxWidth:int;
		
		public function ToolBar(w:int):void {
			groupList = new Array();
			groupMap = new Array();
			left = top = 0;
			maxWidth = w;
			addGroup(new ModeGroup(Config.MODE_GROUP));
			addGroup(new SystemGroup(Config.SYSTEM_GROUP));
			addGroup(new OperateGroup(Config.OPERATE_GROUP));
			addGroup(new DoGroup(Config.DO_GROUP));
			addGroup(new BIUGroup(Config.BIU_GROUP));
			addGroup(new AlignGroup(Config.ALIGN_GROUP));
			addGroup(new FontGroup(Config.FONT_GROUP));
		}
		
		private function addGroup(group:ButtonGroup):void {
			//超过最大宽度后换行
			if (left + group.width >= maxWidth) {
				top += 24;
				left = 0;
			}
			//防止名称冲突
			if(groupList[group.groupName] == null) {
				groupMap[group.groupName] = group;
				groupList.push(group);
			}
			//每行按钮组随后排列
			group.x = left;
			group.y = top;
			left += group.width;
			addChild(group);
		}
		
		public function showGroup(start:int = 0):void {
			for (var i:int = start; i < groupList.length; i++) {
				groupList[i].showButton();
			}
		}
		public function hideGroup(start:int = 0):void {
			for (var i:int = start; i < groupList.length; i++) {
				groupList[i].hideButton();
			}
		}
		public function activeButton(gName:String, bName:String):void {
			groupMap[gName].activeButton(bName);
		}
		public function cancelButton(gName:String, bName:String):void {
			groupMap[gName].cancelButton(bName);
		}
		public function activeBtnInGroup(gName:String, bName:String):void {
			groupMap[gName].activeOne(bName);
		}
		public function cancelGroup(gName:String):void {
			groupMap[gName].cancelAll(null);
		}
		
		public function resize(w:int):void {
			left = top = 0;
			maxWidth = w;
			var len:int = groupList.length;
			while (len--) {
				addGroup(groupList.shift());
			}
		}
		
		//特殊的设置按钮方法
		public function setColorBtn(color:uint = 0):void {
			groupMap[Config.FONT_GROUP].btnMap[Config.FONT_COLOR].setDisplayColor(color);
		}
		public function setSizeBtn(size:int = 12):void {
			groupMap[Config.FONT_GROUP].btnMap[Config.FONT_SIZE].setDisplaySize(size);
		}
		public function setFamilyBtn(font:String = "宋体") {
			groupMap[Config.FONT_GROUP].btnMap[Config.FONT_FAMILY].setDisplayFamily(font);
		}
	}
}